package com.hxc.shop.service;

import com.hxc.shop.bean.ShopCart;
import com.hxc.shop.bean.ShopGoodsInfo;
import com.hxc.shop.common.ResultCode;
import com.hxc.shop.common.ResultVO;
import com.hxc.shop.mapper.Mysql.CartMapper;
import com.hxc.shop.mapper.Mysql.GoodsInfoMapper;
import com.hxc.shop.mapper.Mysql.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class CartService {
    @Resource
    private CartMapper cartMapper;
    @Resource
    private GoodsInfoMapper goodsInfoMapper;
    @Resource
    private UserMapper userMapper;

    public ResultVO getCartInfoByUserName(String userName) {
        if (StringUtils.isEmpty(userName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"用户名为空,获取购物车数据失败!");
        }
        // 判断用户是否存在
        String checkUserName = "";
        try {
            checkUserName = userMapper.findUserNameByUserName(userName);
        }catch (Exception e) {
            log.error("获取购物车信息查找用户失败:"+userName+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找用户失败:"+e.getMessage());
        }
        if (StringUtils.isEmpty(checkUserName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此用户,获取购物车失败!");
        }
        List<ShopCart> shopCartList = new ArrayList<>();
        try {
            shopCartList = cartMapper.getCartInfoByUserName(userName);
        }catch (Exception e) {
            log.error("获取购物车数据失败:"+userName+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"获取购物车数据失败:"+e.getMessage());
        }
        // 动态更新商品单价和总价
        List<ShopCart> updateShopCartList = new ArrayList<>();
        if (shopCartList.size()!=0) {
            for (ShopCart list:shopCartList) {
                ShopCart shopCart = new ShopCart();
                // 根据商品id获取商品信息
                ShopGoodsInfo shopGoodsInfo = new ShopGoodsInfo();
                try {
                    shopGoodsInfo = goodsInfoMapper.getGoodsById(list.getGoodsId());
                }catch (Exception e) {
                    log.error("根据商品id获取商品信息失败:"+list.getGoodsId()+"=="+e.getMessage());
                    continue;
                }
                if (shopGoodsInfo!=null) {
                    shopCart.setGoodsPrice(shopGoodsInfo.getGoodsPrice());
                    shopCart.setGoodsAllPrice(list.getGoodsCount() * shopGoodsInfo.getGoodsPrice());
                }
                updateShopCartList.add(shopCart);
            }
        }
        return new ResultVO(ResultCode.SUCCESS,shopCartList);
    }

    public ResultVO insertCartInfo (ShopCart cart) {
        if (cart == null) {
            return new ResultVO(ResultCode.ERROR.getCode(),"购物车数据为空,新增失败!");
        }
        if (StringUtils.isEmpty(cart.getUserName())) {
            return new ResultVO(ResultCode.ERROR.getCode(),"用户名为空，新增购物车商品失败!");
        }
        if (StringUtils.isEmpty(cart.getGoodsId())) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品编号为空，新增购物车商品失败!");
        }
        if (StringUtils.isEmpty(cart.getGoodsCount()) || cart.getGoodsCount() == 0) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品数量为空,新增购物车商品失败!");
        }
        // 检查用户
        String checkUserName = "";
        try {
            checkUserName = userMapper.findUserNameByUserName(cart.getUserName());
        }catch (Exception e) {
            log.error("插入到购物车查找用户失败:"+cart.getUserName()+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找用户失败:"+e.getMessage());
        }
        if (StringUtils.isEmpty(checkUserName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此用户,新增购物车失败!");
        }
        // 检查商品编号
        Integer checkGoodsId = 0;
        try {
            checkGoodsId = goodsInfoMapper.findGoodsIdByGoodsId(cart.getGoodsId());
        }catch (Exception e) {
            log.error("插入到购物车查找商品编号失败:"+cart.getGoodsId()+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找商品编号失败:"+e.getMessage());
        }
        if (checkGoodsId == null || checkGoodsId == 0) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此商品编号,新增购物车失败!");
        }

        // 检查该商品是否已经在购物车,用户名和商品id检查
        String cartRecord = cartMapper.findCartRecordByUserNameAndGoodsId(cart.getUserName(),cart.getGoodsId());
        if (!StringUtils.isEmpty(cartRecord)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"该商品已经加入购物车!");
        }

        // 插入数据
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cart.setCreateTime(sdf.format(new Date()));
        cart.setUpdateTime(sdf.format(new Date()));
        try {
            cartMapper.insertCart(cart);
        }catch (Exception e) {
            log.error("加入购物车失败:"+cart+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"加入购物车失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS.getCode(),"商品加入购物车成功!");
    }

    public ResultVO updateCartInfo(ShopCart cart) {
        if (cart == null) {
            return new ResultVO(ResultCode.ERROR.getCode(),"需要修改的商品数据为空,更新失败!");
        }
        if (StringUtils.isEmpty(cart.getUserName())) {
            return new ResultVO(ResultCode.ERROR.getCode(),"用户名为空，更新购物车商品失败!");
        }
        if (StringUtils.isEmpty(cart.getGoodsId())) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品编号为空，更新购物车商品失败!");
        }
        if (StringUtils.isEmpty(cart.getGoodsCount()) || cart.getGoodsCount() == 0) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品数量为空,更新购物车商品失败!");
        }
        // 检查用户
        String checkUserName = "";
        try {
            checkUserName = userMapper.findUserNameByUserName(cart.getUserName());
        }catch (Exception e) {
            log.error("插入到购物车查找用户失败:"+cart.getUserName()+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找用户失败:"+e.getMessage());
        }
        if (StringUtils.isEmpty(checkUserName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此用户,新增购物车失败!");
        }
        // 检查商品编号
        Integer checkGoodsId = 0;
        try {
            checkGoodsId = goodsInfoMapper.findGoodsIdByGoodsId(cart.getGoodsId());
        }catch (Exception e) {
            log.error("插入到购物车查找商品编号失败:"+cart.getGoodsId()+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找商品编号失败:"+e.getMessage());
        }
        if (checkGoodsId == null || checkGoodsId == 0) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此商品编号,新增购物车失败!");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cart.setUpdateTime(sdf.format(new Date()));
        try {
            cartMapper.updateCartInfo(cart);
        }catch (Exception e) {
            log.error("更新购物车商品失败:"+cart+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"更新购物车商品失败:"+e.getMessage());
        }

        return new ResultVO(ResultCode.SUCCESS.getCode(),"更新购物车商品成功!");
    }

    public ResultVO deleteCartInfo(String userName,Integer id) {
        if (StringUtils.isEmpty(userName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"用户名为空，删除购物车失败!");
        }
        if (id == 0 || id == null) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品编号为空，删除购物车失败!");
        }
        // 检查用户
        String checkUserName = "";
        try {
            checkUserName = userMapper.findUserNameByUserName(userName);
        }catch (Exception e) {
            log.error("插入到购物车查找用户失败:"+userName+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找用户失败:"+e.getMessage());
        }
        if (StringUtils.isEmpty(checkUserName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此用户,删除购物车失败!");
        }
        // 检查商品编号
        Integer checkGoodsId = 0;
        try {
            checkGoodsId = goodsInfoMapper.findGoodsIdByGoodsId(id);
        }catch (Exception e) {
            log.error("插入到购物车查找商品编号失败:"+id+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找商品编号失败:"+e.getMessage());
        }
        if (checkGoodsId == null || checkGoodsId == 0) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此商品编号,删除购物车失败!");
        }
        try {
            cartMapper.deleteCart(userName,id);
        }catch (Exception e) {
            log.error("删除购物车商品失败:"+userName+"=="+id+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"删除购物车商品失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS.getCode(),"删除购物车商品成功!用户名:"+userName+",商品编号:"+id);
    }

    // 根据商品名称获取购物车商品数据
    public ResultVO getCartGoodsByGoodsName(String goodsName,String userName) {
        if (StringUtils.isEmpty(goodsName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品名称为空，查找失败!");
        }
        if (StringUtils.isEmpty(userName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"用户名为空，查找失败!");
        }
        // 判断用户是否存在
        String checkUserName = "";
        try {
            checkUserName = userMapper.findUserNameByUserName(checkUserName);
        }catch (Exception e) {
            log.error("获取购物车信息查找用户失败:"+userName+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找用户失败:"+e.getMessage());
        }
        if (StringUtils.isEmpty(checkUserName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此用户,获取购物车失败!");
        }

        List<ShopCart> shopCartList = new ArrayList<>();
        try {
            shopCartList = cartMapper.getCartInfoByUserName(userName);
        }catch (Exception e) {
            log.error("获取购物车数据失败:"+userName+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"获取购物车数据失败:"+e.getMessage());
        }
        List<ShopCart> checkShopCartList = new ArrayList<>();
        for (ShopCart cart:shopCartList) {
            if (cart.getGoodsName().contains(goodsName)) {
                checkShopCartList.add(cart);
            }
        }
        return new ResultVO(checkShopCartList);
    }

}
